#include "MediaInfoObserver.h"
#include <pthread.h>
#include "Log.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/system_properties.h>


void (*pOnMediaLibraryChanged)(struct MediaMetadata * metadata );
pthread_t 		m_MediaActiveTid;
pthread_t *		m_MediaActiveThreadPtr;

pthread_t 		m_MediaMetadataTid;
pthread_t *		m_MediaMetadataThreadPtr;

int globalActiveStatus = 0; //target audio channel
int lastActiveStatus = 0;
volatile int keepMediaActiveRunning = 1;

char lastTitle [ 1024 ] = { 0 };
long elapsedTime = 0;
volatile int keepObserveMediaMetadataRunning = 0;

void *mediaActiveThread(void *arg) {
	char	activeStatus [ 100 ];
	keepMediaActiveRunning = 1;
	while ( keepMediaActiveRunning ){

		memset(activeStatus,0,100);
		__system_property_get(METADATA_KEY_STATE, activeStatus);
		if(strlen(activeStatus)==0){
			strncpy(activeStatus,"0",strlen("0"));
		}
		int newActiveStatus = atoi( activeStatus );
		if( newActiveStatus != globalActiveStatus )
		{
			globalActiveStatus = newActiveStatus;
			LOGD("the app set new media active = %d \n", globalActiveStatus);
			if( newActiveStatus == 3 )
			{
				//start开始更新媒体库
				elapsedTime = 0;
				if(pOnMediaLibraryChanged != NULL)
				{
					struct MediaMetadata metadata;
					char	duration [ 100 ];
					__system_property_get(METADATA_KEY_TITLE,  metadata.title);
					__system_property_get(METADATA_KEY_ARTIST, metadata.artist);
					__system_property_get(METADATA_KEY_ALBUM,  metadata.album);
					__system_property_get(METADATA_KEY_DURATION, duration);
					metadata.duration =  atol( duration );
					metadata.playbackStatus = 1;
					metadata.elapsedTime = elapsedTime;
					//Check legal parameter
					if(strlen( metadata.title ) == 0 )
					{
						strcpy(metadata.title,"Unknown Title");
					}
					if(strlen( metadata.artist ) == 0 )
					{
						strcpy(metadata.artist,"Unknown Artist");
					}
					if(strlen( metadata.album ) == 0 )
					{
						strcpy(metadata.album,"Unknown Album");
					}
					LOGD("pOnMediaLibraryChanged title = %s \n", metadata.title);
					LOGD("pOnMediaLibraryChanged artist = %s \n", metadata.artist);
					LOGD("pOnMediaLibraryChanged duration = %s \n", duration);
					LOGD("pOnMediaLibraryChanged metadata.duration = %ld \n", metadata.duration);

					pOnMediaLibraryChanged(&metadata);

				}
				keepObserveMediaMetadataRunning = 1;
			}else{
				//stop 就停止更新媒体库
				lastActiveStatus = globalActiveStatus;
				keepObserveMediaMetadataRunning = 0;

				if(pOnMediaLibraryChanged != NULL)
				{
					struct MediaMetadata metadata;
					char	duration [ 100 ];
					__system_property_get(METADATA_KEY_TITLE, metadata.title);
					__system_property_get(METADATA_KEY_ARTIST, metadata.artist);
					__system_property_get(METADATA_KEY_ALBUM, metadata.album);
					__system_property_get(METADATA_KEY_DURATION, duration);

					//Check legal parameter
					if(strlen( metadata.title ) == 0 )
					{
						strcpy(metadata.title,"Unknown Title");
					}
					if(strlen( metadata.artist ) == 0 )
					{
						strcpy(metadata.artist,"Unknown Artist");
					}
					if(strlen( metadata.album ) == 0 )
					{
						strcpy(metadata.album,"Unknown Album");
					}

					metadata.duration =  atol( duration );
					// 0=Stopped , 1=Playing , 2=Pause
					metadata.playbackStatus = 2;
					pOnMediaLibraryChanged(&metadata);

				}
			}
		}
		usleep(200*1000);
	}

	return (NULL);
}

void *mediaMetadataThread(void *arg) {
	char	title [ 1024 ];
	keepMediaActiveRunning = 1;
	char	activeStatus [ 100 ] = { 0 };
	while ( keepMediaActiveRunning ){
//		LOGD("mediaMetadataThread running...\n");
		while ( keepObserveMediaMetadataRunning ){
			memset(title,0,100);
			__system_property_get(METADATA_KEY_TITLE, title);
			if(strlen(title) == 0 ){
				strncpy(title,"Unknown Title",strlen("Unknown Title"));
			}
			if( strcmp (lastTitle, title) != 0 )
			{
				memcpy(lastTitle,title, 1024 );

				LOGD("mediaMetadataThread the app set new title = %s \n", lastTitle);
				if(pOnMediaLibraryChanged != NULL)
				{
					struct MediaMetadata metadata;
					char	duration [ 100 ];
					__system_property_get(METADATA_KEY_TITLE,  metadata.title);
					__system_property_get(METADATA_KEY_ARTIST, metadata.artist);
					__system_property_get(METADATA_KEY_ALBUM,  metadata.album);
					__system_property_get(METADATA_KEY_DURATION, duration);


					memset(activeStatus,0,100);
					__system_property_get(METADATA_KEY_STATE, activeStatus);
					int newActiveStatus = 0;
					if(strlen( activeStatus ) > 0 )
					{
						newActiveStatus = atoi( activeStatus );
					}

					if( newActiveStatus == 3 )
					{
						metadata.playbackStatus = 1;
					}else{
						metadata.playbackStatus = 2;
					}

					metadata.duration =  atol( duration );

					elapsedTime += 10000;
					metadata.elapsedTime = elapsedTime;
					//Check legal parameter
					if(strlen( metadata.title ) == 0 )
					{
						strcpy(metadata.title,"Unknown Title");
					}
					if(strlen( metadata.artist ) == 0 )
					{
						strcpy(metadata.artist,"Unknown Artist");
					}
					if(strlen( metadata.album ) == 0 )
					{
						strcpy(metadata.album,"Unknown Album");
					}
					LOGD("pOnMediaLibraryChanged title = %s \n", metadata.title);
					LOGD("pOnMediaLibraryChanged artist = %s \n", metadata.artist);
					LOGD("pOnMediaLibraryChanged duration = %s \n", duration);
					LOGD("pOnMediaLibraryChanged metadata.duration = %ld \n", metadata.duration);

					pOnMediaLibraryChanged(&metadata);

				}
			}
			usleep(10000*1000); //1s一次
		}
		usleep(1000*1000); //1S
	}

	return (NULL);
}


void start_observe_media_info(void (*pFunc)(struct MediaMetadata * metadata)){
	pOnMediaLibraryChanged = pFunc;
	int ret;
	if ((ret = pthread_create(&m_MediaActiveTid, NULL, mediaActiveThread, NULL))
			!= 0) {
		LOGD("observe Media Active Status thread create failed!\n");
	} else {
		m_MediaActiveThreadPtr = &m_MediaActiveTid;
		LOGD("observe Media Active Status thread create successed!\n");
	}

	int ret2;
	if ((ret2 = pthread_create(&m_MediaMetadataTid, NULL, mediaMetadataThread, NULL))
			!= 0) {
		LOGD("observe Media Info Library thread create failed!\n");
	} else {
		m_MediaMetadataThreadPtr = &m_MediaMetadataTid;
		LOGD("observe Media Info Library thread create successed!\n");
	}
}


void cancel_observe_media_info( ){
	keepObserveMediaMetadataRunning = 0;
	keepMediaActiveRunning = 0;
	if( m_MediaActiveThreadPtr !=NULL )
	{
		pthread_join(m_MediaActiveTid , NULL);
		m_MediaActiveThreadPtr = NULL;
	}
	if( m_MediaMetadataThreadPtr !=NULL )
	{
		pthread_join(m_MediaMetadataTid , NULL);
		m_MediaMetadataThreadPtr = NULL;
	}

}


